package tomoto.util;

import java.util.ArrayList;
import java.util.List;

public class CombinationUtil<T> {
    public static <T> List<List<T>> permutation(List<T> list) {
        return permutation(list, list.size());
    }

    public static <T> List<List<T>> permutation(List<T> list, int size) {
        List<List<T>> result = new ArrayList<List<T>>();
        if (size > 0) {
            for (T e : list) {
                List<T> sublist = new ArrayList<T>(list);
                sublist.remove(e);
                for (List<T> subpermutation : permutation(sublist, size - 1)) {
                    subpermutation.add(0, e);
                    result.add(subpermutation);
                }
            }
        } else {
            result.add(new ArrayList<T>());
        }
        return result;
    }
}
